package com.novelastudios.application
{
	import com.novelastudios.namespaces.ns_internal;
	
	import flash.display.DisplayObject;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	
	use namespace ns_internal;
	/**
	 * Parameters as defined by the instantiation of the SWF in the html page
	 * 
	 * @example On the root / stage
	 * 	<listing version="3.0">
	 * 		var initParams:InitParams   = new InitParams(this);
	 * 		var userID:String			= initParams.getItem("userID","-1");
	 * 	</listing>
	 * 
	 * @author John Bailey
	 * 
	 */	
	public class InitParams
	{
		/**
		 * @private
		 */
		protected var _rootRef:DisplayObject;
		
		/**
		 * @private
		 */
		private var _data:Object;
		
		public function InitParams(root:Sprite)
		{
			init(root);
		}
		
		/**
		 * Invoked on Construction
		 */		
		protected function init(root:Sprite):void
		{
			_rootRef = root;
			_data = LoaderInfo(_rootRef.loaderInfo).parameters;
		}
		
		///////////////////////////////////////////////////////////////////////////////
		//								PUBLIC METHOD
		///////////////////////////////////////////////////////////////////////////////
		
		/**
		 * 
		 * @param key
		 * @return  
		 */		
		public function hasItem(key:String):Boolean
		{
			return ( _data && _data.hasOwnProperty(key) )
		}
		
		/**
		 * 
		 * @param key
		 * @param defaultReturn If the key is not found, then this will be returned. 
		 * @return 
		 */		
		public function getItem(key:String,defaultReturn:String=null):String
		{
			return hasItem(key) ? _data[key] : defaultReturn;
		}
		
		/**
		 * Basically, get a clone of the source as an associative array
		 * 
		 */		
		public function getKeyedItems():Array
		{
			var arr:Array = [];
			
			for( var key:String in _data )
			{
				arr[key] = _data[key];
			}
			
			return arr;
		}
		
		
		/**
		 * Use with Extreme caution 
		 * 
		 */		
		ns_internal function setSourceData(value:Object):void
		{
			_data = value ? value : {};
		}
		
	}
}